package tron.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class TronRedisService {
    @Autowired
    RedisTemplate redisTemplate;

    public void test() {
        for (int i = 0; i < 1; i++) {
            new TronThread(redisTemplate).start();
        }
    }


    public class TronThread extends Thread{
        private boolean isGood = true;
        private RedisTemplate redisTemplate;
        public TronThread(RedisTemplate redisTemplate) {
            this.redisTemplate = redisTemplate;
        }

        @Override
        public void run() {
            while (isGood) {
                try {
                    Integer test = (Integer) redisTemplate.opsForValue().get("test");
                    System.out.println(Thread.currentThread().getId()+"操作修改方法："+test);
                    test += 1;
                    redisTemplate.opsForValue().set("test", test);
                } catch (Exception e) {
                    e.printStackTrace();
                    isGood = false;
                }
            }
        }
    }
}
